package com.whut.whw.personalinfo.mywant;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.whut.whw.R;
import com.whut.whw.model.Goods;
import com.whut.whw.util.ConvertUtils;
import com.whut.whw.util.EmptyUtils;
import com.whut.whw.util.ScreenUtils;
import com.whut.whw.util.TimeUtils;

import java.util.List;

import static com.whut.whw.constant.ConfigConsts.IMG_URL;

/**
 * <pre>
 *     author : 杨丽金
 *     time   : 2017/06/25
 *     desc   : 个人中心-“我感兴趣的”界面列表适配器
 *     version: 1.0
 * </pre>
 */
public class MyWantRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements SlidingButtonView.IonSlidingButtonListener {

    private Context mContext;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;

    public List<Goods> getmDatas() {
        return mDatas;
    }

    private List<Goods> mDatas;
    private SlidingButtonView mMenu = null;  // 保存整个item根布局：SlidingButtonView
    private static final int TYPE_ITEM = 0;// item布局
    private static final int TYPE_FOOTER = 1;// 尾布局
    private boolean footerFlag = false;

    public void setFooterFlag(boolean footerFlag) {
        this.footerFlag = footerFlag;
    }

    public boolean isFooterFlag() {
        return footerFlag;
    }

    public MyWantRecyclerViewAdapter(Context context, List<Goods> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
    }

    /**
     * 设置监听事件
     *
     * @param mIDeleteBtnClickListener
     */
    public void setmIDeleteBtnClickListener(IonSlidingViewClickListener mIDeleteBtnClickListener) {
        this.mIDeleteBtnClickListener = mIDeleteBtnClickListener;
    }

    /**
     * 更新数据源
     *
     * @param mDatas
     */
    public void updateData(List<Goods> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

    /**
     * 向数据源中追加数据
     *
     * @param mDatas
     */
    public void appendData(List<Goods> mDatas) {
        this.mDatas.addAll(mDatas);
        notifyDataSetChanged();
    }

    /**
     * 删除某一条目
     *
     * @param position
     */
    public void removeData(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }


    @Override
    public int getItemCount() {
        //默认刚进入有加载
        return EmptyUtils.isEmpty(mDatas.size()) ? 0 : mDatas.size() + 1;
//        return mDatas.size() == 0 ? 0 : mDatas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_mywant, parent,
                    false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.default_loading, parent,
                    false);
            return new FootViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            Goods informModel = mDatas.get(position);
            final ItemViewHolder itemViewHolder = (ItemViewHolder) holder;


            Glide.with(mContext).load(IMG_URL + (informModel.getZp().split(";"))[0]).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>(ConvertUtils.dp2px(60), ConvertUtils.dp2px(60)) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                    itemViewHolder.mIvImage.setImageBitmap(resource);
                }
            });
//            if (EmptyUtils.isNotEmpty(informModel.getZp())) {
//
//            } else {
//                Glide.with(mContext).load(R.drawable.logo).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>(ConvertUtils.dp2px(60), ConvertUtils.dp2px(60)) {
//                    @Override
//                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                        itemViewHolder.mIvImage.setImageBitmap(resource);
//                    }
//                });
//            }
//
//            Glide.with(mContext).load(entity.getImg_url()).asBitmap().placeholder(R.mipmap.ic_launcher).error(R.drawable.girl).centerCrop().into(new SimpleTarget<Bitmap>(ConvertUtils.dp2px(60), ConvertUtils.dp2px(60)) {
//                @Override
//                public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//                    itemViewHolder.mIvImage.setImageBitmap(resource);
//                }
//            });
            itemViewHolder.myTvTitle.setText(informModel.getBt());
            itemViewHolder.myTvLikes.setText(informModel.getDzsl() + "");
            itemViewHolder.myTvComment.setText(informModel.getPlsl() + "");
            itemViewHolder.myTvTime.setText(TimeUtils.date2String(informModel.getFbsj()));

            //设置内容布局的宽为屏幕宽度
            itemViewHolder.mVgLayoutContent.getLayoutParams().width = ScreenUtils.getScreenWidth((Activity) mContext);

            itemViewHolder.mVgLayoutContent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //判断是否有删除菜单打开
                    if (menuIsOpen()) {
                        closeMenu();//关闭菜单
                    } else {
                        if (mIDeleteBtnClickListener != null) {
                            int n = holder.getLayoutPosition();
                            mIDeleteBtnClickListener.onItemClick(v, n);
                        }
                    }
                }
            });


            itemViewHolder.mTvDelete.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (mIDeleteBtnClickListener != null) {
                        int n = holder.getLayoutPosition();
                        mIDeleteBtnClickListener.onDeleteBtnCilck(v, n);
                    }
                }
            });
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footHolder = (FootViewHolder) holder;
            if (footerFlag) {
                footHolder.loading_liearlayout.setVisibility(View.GONE);
                footHolder.loading_finished_linearlayout.setVisibility(View.VISIBLE);
            } else {
                footHolder.loading_liearlayout.setVisibility(View.VISIBLE);
                footHolder.loading_finished_linearlayout.setVisibility(View.GONE);
            }
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        ImageView mIvImage;
        TextView myTvTitle;
        TextView myTvLikes;
        TextView myTvComment;
        TextView myTvTime;

        ViewGroup mVgLayoutContent;

        // 删除按钮
        TextView mTvDelete;

        public ItemViewHolder(View view) {
            super(view);
            mIvImage = (ImageView) view.findViewById(R.id.mywant_img_iv);
            myTvTitle = (TextView) view.findViewById(R.id.mywant_title_tv);
            myTvLikes = (TextView) view.findViewById(R.id.mywant_likes_tv);
            myTvComment = (TextView) view.findViewById(R.id.mywant_comment_tv);
            myTvTime = (TextView) view.findViewById(R.id.mywant_time_tv);

            mVgLayoutContent = (ViewGroup) view.findViewById(R.id.layout_content);
            mTvDelete = (TextView) view.findViewById(R.id.tv_delete);

            // 整个itemview是SlidingButtonView的对象，为整个item设置监听事件
            ((SlidingButtonView) view).setSlidingButtonListener(MyWantRecyclerViewAdapter.this);
        }
    }

    // 尾布局ViewHolder
    class FootViewHolder extends RecyclerView.ViewHolder {

        LinearLayout loading_finished_linearlayout;
        LinearLayout loading_liearlayout;

        public FootViewHolder(View view) {
            super(view);
            loading_finished_linearlayout = (LinearLayout) itemView.findViewById(R.id.loading_finished_linearlayout);
            loading_liearlayout = (LinearLayout) itemView.findViewById(R.id.loading_linearlayout);
        }
    }

    /**
     * 删除菜单打开信息接收
     */
    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    /**
     * 滑动或者点击了Item监听
     *
     * @param slidingButtonView
     */
    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }


    /**
     * 判断是否有菜单打开
     */
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        return false;
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDeleteBtnCilck(View view, int position);
    }

}
